package com.zl.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhanglei2
 * @since 2019/12/12 16:57
 */
public class LoginHandlerInterceptor implements HandlerInterceptor  {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取用户登录信息如果登录了会将登录信息存在session中,如果session中没有返回到登录页面,如果存在则放行
        Object user = request.getSession().getAttribute("user");
        if(user == null){
            request.setAttribute("msg","用户未登录,请登录!");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }
        return true;
    }
}
